var _currentData = null;
var _controls = null;
function _clearValue() {
	if (_controls != null && typeof(_controls) != 'undefined' && _controls.length > 0) {
		for (var i = 0; i < _controls.length; i++) {
			var temp = $('#' + _controls[i]).attr("defaultValue");
			if (temp != null && typeof(temp) != 'undefined') {
				$('#' + _controls[i]).val(temp);
			} else {
				$('#' + _controls[i]).val('');
			}
		}
	}
}
function _bindControls(array) {
	_controls = array;
}
function _loadData(url, param, func) {
	if (typeof(func) == 'undefined' || func == null) {
		func = function(data) {
			if (_controls != null && typeof(_controls) != 'undefined' && _controls.length > 0) {
				for (var i = 0; i < _controls.length; i++) {
					$('#' + _controls[i]).val(data[_controls[i]]);
				}
			}
		};
	}
	$.post(url, param, function(data) {
				_currentData = data.rows[0];
				func(_currentData);
        	}, "json");
}
function _resetChange() {
	if (_currentData != null && typeof(_currentData) != "undefined") {
		_setValue(_currentData);
	} else {
		_clearValue();
	}
}
function _submitChange(url, param, isJson) {
	if (typeof(param) == 'undefined') {
	    var _param = new Object();
	    if (_controls != null && typeof(_controls) != 'undefined' && _controls.length > 0) {
	        for (var i = 0; i < _controls.length; i++) {
	            var tempId = _controls[i];
	            if ($('#' + tempId).attr('required') == 'true' && !validate(tempId)) {
            		optimus.Window.alert('错误提示', '资源路径不能为空', 'error');
            		return false;
	            } else {
	            	_param[tempId] = $('#' + tempId).val();
	            }
	        }
	    }
	    param = _param
	}
	var contentType = "application/x-www-form-urlencoded";
	if (isJson == true) {
		contentType = "application/json";
		param = $.toJSON(param);
	}
	$.ajax({
		type: "POST",
		dataType: 'json',
		url: url,
		data: param,
		contentType: contentType,
		cache: false,
		success: function(data, textStatus){
			if (data.result != 'success') {
				var parentFunc = window.parent.onSubmitChangeFailure;
				var localFunc = window.onSubmitChangeFailure;
				if ($.isFunction(parentFunc)) {
					parentFunc();
				} else if ($.isFunction(localFunc)) {
					localFunc();
				}
			} else {
				var parentFunc = window.parent.onSubmitChangeSuccess;
				var localFunc = window.onSubmitChangeSuccess;
				if ($.isFunction(parentFunc)) {
					parentFunc();
				} else if ($.isFunction(localFunc)) {
					localFunc();
				}
			}
		}, 
		error: function(jqXHR, textStatus, errorThrown) {
			optimus.Window.alert("操作提示", "系统错误");
		}
	});
}

function onSubmitChangeSuccess() {
	optimus.Window.alert("操作提示", "操作成功");
}

function onSubmitChangeFailure() {
	optimus.Window.alert("操作提示", "操作失败", "error");
}

function _onCancel() {
	var parentFunc = window.parent.cancelSaveOrUpdate;
	if ($.isFunction(parentFunc)) {
		parentFunc();
	}
}